package vg.modules.util_plugins;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphRenderer;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions.DefaultAction;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions.DefaultActionAdapter;
import vg.services.main_manager.MainManager;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;
import vg.services.user_interface_manager.interfaces.UserInterfaceObserverAdapter;

public class PanningButtonPlugin extends AUserInterfaceElement implements IPlugin {
	// Main data
	private AGraphView view;
	private JToolBar toolBar;
	private JToggleButton button;
	
	// Mutex
	private final Object generalMutex = new Object();
	
	public PanningButtonPlugin() {
		super("PanningButtonPlugin", null);
	}
	
	@Override
	public void install(final PluginParameter pluginParameter) throws Exception {
        view  = null;
        button = new JToggleButton(new ImageIcon("./data/resources/textures/drag_hand.png"));
        button.setToolTipText("Panning graph view");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                synchronized (generalMutex) {
                    if (view!= null) {
                        view.addAction(new DefaultActionAdapter("panning") {
                            @Override
                            public void action(final IGraphRenderer graphRenderer) {
                                graphRenderer.setPanning(!graphRenderer.isPanning());
                            }
                        });
                    }
                }
            }
        });

        // build toolbar
        toolBar = new JToolBar();
        toolBar.setFloatable(false);
        toolBar.add(button);

        pluginParameter.userInterfaceManager.addObserver(new UserInterfaceObserverAdapter() {
            @Override
            public void dispatchEvent(AUIEvent event) {
                switch (event.getType()) {
                    case DEF_CHANGE_UI_STYLE: {
                        synchronized (generalMutex) {
                            SwingUtilities.updateComponentTreeUI(toolBar);
                        }
                        break;
                    }
                    case DEF_CHANGE_TAB: {
                        UIEventChangeTab bufEvent = (UIEventChangeTab) event;
                        ATab tab = bufEvent.getTab();
                        if (tab == null)
                            return;

                        switch (tab.getTabType()) {
                            case ATab.DEF_GRAPH_VIEW_TAB_TYPE:
                                synchronized (generalMutex) {
                                    view = (AGraphView)tab;
                                    view.addAction(new DefaultActionAdapter("panning") {
                                        @Override
                                        public void action(final IGraphRenderer graphRenderer) {
                                            graphRenderer.setPanning(button.isSelected());
                                        }
                                    });
                                }
                                break;

                            default:
                                break;
                        }

                        break;
                    }
                    default:
                        break;
                }
            }
        });

        // add toolbar to instruments
        pluginParameter.userInterfaceManager.addInstrument(this);
	}

	@Override
	public JComponent getView() {
		synchronized (generalMutex) {
            return toolBar;
        }
	}

}
